package com.dungnt.vtime;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;


import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dungnt.vtime.alarm.AlarmFragment;
import com.dungnt.vtime.common.cls.VTViewPager;
import com.dungnt.vtime.countdown.VTCountDownFragment;
import com.dungnt.vtime.stopwatch.VTStopWatchFragment;

public class VTMainActivity extends Activity {

	public static ActionBar mBar;
	private VTViewPager mViewPager;
	private TabAdapter mTabAdapter;
	private View mStopWatchCustomTabView;
	private View mCountdownCustomTabView;
	private View mAlarmCustomTabView;
	private View mEventCustomTabView;
	private int mOrientation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mViewPager = new VTViewPager(this);
		mViewPager.setId(0x1000);
		setContentView(mViewPager);
		initUI();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private void initUI() {
		ActionBar bar = getActionBar();
		bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		bar.setDisplayOptions(0);
		mTabAdapter = new TabAdapter(this, mViewPager);
		mBar = bar;
		mStopWatchCustomTabView = getLayoutInflater().inflate(
				R.layout.actionbar_custom_tab, null);
		mCountdownCustomTabView = getLayoutInflater().inflate(
				R.layout.actionbar_custom_tab, null);
		mAlarmCustomTabView = getLayoutInflater().inflate(
				R.layout.actionbar_custom_tab, null);
		mEventCustomTabView = getLayoutInflater().inflate(
				R.layout.actionbar_custom_tab, null);
		mTabAdapter.addTab(
				getCustomTab(bar, mStopWatchCustomTabView,
						R.drawable.stopwatch_tab, R.string.tab_stopwatch),
				VTStopWatchFragment.class, null);
		mTabAdapter.addTab(
				getCustomTab(bar, mCountdownCustomTabView,
						R.drawable.stopwatch_tab, R.string.tab_countdonw),
				VTCountDownFragment.class, null);
		mTabAdapter.addTab(
				getCustomTab(bar, mAlarmCustomTabView,
						R.drawable.stopwatch_tab, R.string.tab_alarm),
				AlarmFragment.class, null);
		mTabAdapter.addTab(
				getCustomTab(bar, mEventCustomTabView,
						R.drawable.stopwatch_tab, R.string.tab_event),
				VTCountDownFragment.class, null);
		int idTitle = R.id.tab_custom_view_text;
		for (int tabindex = 0; tabindex < bar.getTabCount(); tabindex++) {
			TextView tv = (TextView) bar.getTabAt(tabindex).getCustomView()
					.findViewById(idTitle);
			tv.setTextColor(getResources()
					.getColor(R.color.tab_widget_unselect));
			tv.setShadowLayer(0, 0, 0, R.color.tab_widget_text_shadow);
		}

		TextView tv = (TextView) bar.getSelectedTab().getCustomView()
				.findViewById(idTitle);
		tv.setTextColor(getResources().getColor(R.color.tab_widget_select));
		tv.setShadowLayer(2.0f, 1.0f, 1.0f, R.color.tab_widget_text_shadow);
		mOrientation = getResources().getConfiguration().orientation;
	}

	private ActionBar.Tab getCustomTab(ActionBar bar, View customTab, int icon,
			int text) {
		final ActionBar.Tab tab = bar.newTab();
		((ImageView) customTab.findViewById(R.id.tab_custom_view_icon))
				.setImageResource(icon);
		((TextView) customTab.findViewById(R.id.tab_custom_view_text))
				.setText(text);
		tab.setCustomView(customTab);
		return tab;
	}

	public static class TabAdapter extends FragmentPagerAdapter implements
			TabListener, OnPageChangeListener {

		private final Context mContext;
		private final ActionBar mActionBar;
		private final VTViewPager mViewPager;
		private final ArrayList<TabInfo> mTabs = new ArrayList<VTMainActivity.TabAdapter.TabInfo>();

		static final class TabInfo {
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(Class<?> _class, Bundle _args) {
				clss = _class;
				args = _args;
			}
		}

		public TabAdapter(Activity activity, VTViewPager pager) {
			super(activity.getFragmentManager());
			mContext = activity;
			mActionBar = activity.getActionBar();
			mViewPager = pager;
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		private void addTab(Tab tab, Class<?> clss, Bundle args) {
			TabInfo info = new TabInfo(clss, args);
			tab.setTag(info);
			tab.setTabListener(this);
			mTabs.add(info);
			mActionBar.addTab(tab);
			notifyDataSetChanged();
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			try {
				mActionBar.setSelectedNavigationItem(position);
			} catch (IllegalStateException e) {
			}
		}

		@Override
		public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub
			((Activity) mContext).closeContextMenu();
			Object tag = tab.getTag();
			for (int i = 0; i < mTabs.size(); i++) {
				if (mTabs.get(i) == tag) {
					mViewPager.setCurrentItem(i);
				}
			}

			if (mBar != null) {
				int idTitle = R.id.tab_custom_view_text;
				for (int tabindex = 0; tabindex < mBar.getTabCount(); tabindex++) {
					TextView tv = (TextView) mBar.getTabAt(tabindex)
							.getCustomView().findViewById(idTitle);
					tv.setTextColor(((Activity) mContext).getResources()
							.getColor(R.color.tab_widget_unselect));
					tv.setShadowLayer(0, 0, 0, R.color.tab_widget_text_shadow);
				}

				TextView tv = (TextView) tab.getCustomView().findViewById(
						idTitle);
				tv.setTextColor(((Activity) mContext).getResources().getColor(
						R.color.tab_widget_select));
				tv.setShadowLayer(2.0f, 1.0f, 1.0f,
						R.color.tab_widget_text_shadow);
			}
		}

		@Override
		public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public android.app.Fragment getItem(int position) {
			// TODO Auto-generated method stub
			if (getCount() < position)
				return null;
			TabInfo info = mTabs.get(position);
			android.app.Fragment bookmarkFragment = android.app.Fragment
					.instantiate(mContext, info.clss.getName(), info.args);
			return bookmarkFragment;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mTabs.size();
		}

	}

	private void setLayoutValueAsOrientation() {

		int orientation = (mOrientation == Configuration.ORIENTATION_PORTRAIT) ? LinearLayout.VERTICAL
				: LinearLayout.HORIZONTAL;
		int idText = R.id.tab_custom_view_text;
		int idLayout = R.id.tab_custom_layout;
		int textSize = (int) getResources().getDimension(R.dimen.tab_text_size);

		for (int tabindex = 0; tabindex < mBar.getTabCount(); tabindex++) {
			if (orientation == ((LinearLayout) mBar.getTabAt(tabindex)
					.getCustomView().findViewById(idLayout)).getOrientation())
				((LinearLayout) mBar.getTabAt(tabindex).getCustomView()
						.findViewById(idLayout))
						.setOrientation(orientation == LinearLayout.VERTICAL ? LinearLayout.HORIZONTAL
								: LinearLayout.VERTICAL);

			((LinearLayout) mBar.getTabAt(tabindex).getCustomView()
					.findViewById(idLayout)).setOrientation(orientation);
			((TextView) (mBar.getTabAt(tabindex).getCustomView()
					.findViewById(idText))).setTextSize(
					TypedValue.COMPLEX_UNIT_PX, textSize);
		}
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		if (newConfig.orientation == mOrientation)
			return;
		mOrientation = newConfig.orientation;
		setLayoutValueAsOrientation();
	}
}
